<?php
/*
 * @param author feyeen date 20160425
 * @param address wap2016
 */
namespace wap\controllers;

use Yii;
use services\models\MemberAddress;
use common\widgets\WapAddressWidget;
use common\widgets\SelectWidget;
use yii\helpers\ArrayHelper;
use services\models\MemberInfoBestry;
use yii\base\Exception;
use yii\web\NotFoundHttpException;

class MemberAddressController extends CustomController{
    //地址列表页面
    public function actionList(){
        //验证登录
        if(!self::verification())
            return  $this->redirect($this->login_url);

        //获取当前地址列表
        $MemberAddress  = new MemberAddress();
        $memlist        = $MemberAddress->addressList($this->bestry_uid);

        //补加运费字段
        $info = ArrayHelper::toArray($memlist, ['services\models\MemberAddress' => ['uid','member_uid','s1_uid','s2_uid','s3_uid','consignee','s1','s2','s3','address','zipcode','cell','tel1','tel2','tel3','is_default','addtime','express_charge' => function ($post) {return $post->express_charge;},],]);
        //print_r($info);exit;
        return $this->render('list',['info'=>$info]);
    }
    //新增页面
    public function actionAdd(){
        //会员验证
        if(!self::verification())
            return  $this->redirect($this->login_url);

        //根据uid查询会员信息
        $memberModel = new MemberInfoBestry();
        $memberInfo = $memberModel->find()->select(['uid','true_name','cell'])->where(['uid'=>$this->bestry_uid])->one();
        //分配数据

        return $this->render('newly-added',['memberInfo'=>$memberInfo,'editFlag'=>0]);
    }
    //编辑页面
    public function actionEdit(){
        //会员验证
        if(!self::verification())
            return  $this->redirect($this->login_url);
        //获取数据
        $addressUid     = intval(Yii::$app->request->get('uid'));

        if(empty($addressUid) || $addressUid < 0)
            throw new NotFoundHttpException(true);

        $memberAddress  = new MemberAddress();
        $memberInfo     = $memberAddress->getMemberAddressInfo($this->bestry_uid,$addressUid);

        if(empty($memberInfo))
            throw new NotFoundHttpException(true);

        return $this->render('newly-added',['memberInfo'=>$memberInfo,'editFlag'=>1]);
    }
    //保存新曾
    public function actionSaveAjax(){
        //会员验证
        $this->enableCsrfValidation =false;
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        if(!self::verification())
            return ['data' => '', 'msg' => '没有登录！','flag' => false, 'error_code' => '-401'];
        //地址列表
        $params = Yii::$app->request->post();
        $params['member_uid'] = $this->bestry_uid;
        $flag = $params['editFlag'];
        //判断大于10条不能添加

        $MemberAddress = new MemberAddress();
        if($flag == 0){ //0增加 1编辑
            $count = $MemberAddress->find()->where(['member_uid' => $this->bestry_uid])->count();
            //添加时间
            $params['addtime'] = time();
            if ($count >= 10)
                return ['data' =>'', 'msg' => '收货地址不能超过十个','flag' => false, 'error_code' => '-401'];

            $addressInfo =$MemberAddress->addressSetAdd($params);

        }elseif($flag == 1){
            $params['uid'] = isset($params['uid']) ? $params['uid'] : 0;
            $addressInfo = $MemberAddress->addressSetEdit($params);
        }
        //处理返回参数格式
        if($addressInfo['code'] < 0){
            return ['data'=>'','msg'=>$addressInfo['msg'],'flag'=>false];
        }else{
            return ['data'=>$addressInfo['data'],'msg'=>$addressInfo['msg'],'flag'=>true];
        }
    }

    //删除数据
    public function actionDel(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        //验证用户信息
        if(!self::verification())
            return ['data' => '', 'msg' => '没有登录！','flag' => false];

        //地址列表
        $uid = Yii::$app->request->get('uid');
        if(empty($uid) || !is_numeric($uid))
            return ['data' => '', 'msg' => '参数错误！','flag' => false];

        $MemberAddress = new MemberAddress();
        $addressInfo = $MemberAddress->addressSetDel($this->bestry_uid, $uid);

        if($addressInfo['code'] < 0){
            return ['data'=>'','msg'=>$addressInfo['msg'],'flag'=>false];
        }else{
            return ['data'=>$addressInfo['data'],'msg'=>$addressInfo['msg'],'flag'=>true];
        }
        return $addressInfo;
    }
    //设置默认地址
    public function actionDefault()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回

        if(!self::verification())
            return ['data' => '', 'msg' => '没有登录！','flag' => false];

        $uid = intval(Yii::$app->request->get('uid'));
        if(empty($uid) || $uid < 0)
            return  ['data' =>'', 'msg' => '参数错误！', 'flag'=>false];

        $MemberAddress = new MemberAddress();
        $addressInfo = $MemberAddress->addressSetDefault($this->bestry_uid, $uid);
        //返回列表数据

        return ['data' => $addressInfo, 'msg' => '成功设置默认！', 'flag'=>true];
    }
    //验证登录
    private function verification(){

        if(!$this->isLogin()){
            return false;
        }

        if(empty($this->bestry_uid) || $this->bestry_uid < 0){
             return false;
        }
        return true;
    }

    //获取当前地址信息 order
    public function actionAddressInfo()
    {

        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        $uid = Yii::$app->request->get('uid');
        $MemberAddress = new MemberAddress();

        $addressInfo = $MemberAddress->getMemberAddressInfo($this->bestry_uid, $uid);

        if(isset($addressInfo['code']))
            return '';

        if(count($addressInfo) <= 0)
            return '';
        //获取layer =1城市

        $layerPro = $MemberAddress->getAreaAddress(1, 1);
        $layerProS1 = SelectWidget::widget(['SelectData' => $layerPro, 'value' => $addressInfo->s1_uid, 'isOr' => 0]);

        //获取layer =2城市
        $layerCity = $MemberAddress->getAreaAddress($addressInfo->s1_uid, 2);
        $layerProS2 = SelectWidget::widget(['SelectData' => $layerCity, 'value' => $addressInfo->s2_uid, 'isOr' => 0]);


        //获取layer =3城市
        $layerArea = $MemberAddress->getAreaAddress($addressInfo->s2_uid, 3);
        $layerProS3 = SelectWidget::widget(['SelectData' => $layerArea, 'value' => $addressInfo->s3_uid, 'isOr' => 0]);

        //赋值
        $addressInfo->s1 = $layerProS1;
        $addressInfo->s2 = $layerProS2;
        $addressInfo->s3 = $layerProS3;

        return $addressInfo;

    }

    //地址列表页面order
    public function actionListorder(){
        //验证登录
        if(!self::verification())
            return  $this->redirect($this->login_url);

        //获取当前地址列表
        $MemberAddress  = new MemberAddress();
        $memlist        = $MemberAddress->addressList($this->bestry_uid);

        //历史url
        $historyUrl = Yii::$app->request->getReferrer();

        //补加运费字段
        $info = ArrayHelper::toArray($memlist, ['services\models\MemberAddress' => ['uid','member_uid','s1_uid','s2_uid','s3_uid','consignee','s1','s2','s3','address','zipcode','cell','tel1','tel2','tel3','is_default','addtime','express_charge' => function ($post) {return $post->express_charge;},],]);

        return $this->render('list-order',['info'=>$info,'historyUrl'=>$historyUrl]);
    }
    //三级地址选择
    public function actionArea(){
        $this->layout = false;
        return  $this->render('area');
    }
    //三级地址请求
    public function actionAreaAjax(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回

        $parent_id  = intval(Yii::$app->request->get('parent_id'));
        $layer      = intval(Yii::$app->request->get('layer'));

        $parent_id  = !empty($parent_id) ? $parent_id: 1;
        $layer  = !empty($layer) ? $layer: 1;

        $info       = WapAddressWidget::widget(['parent_id'=>$parent_id,'layer'=>$layer]);

        return ['data'=>$info,'flag'=>true,'msg'=>'请求成功'];

    }

}